package org.pentaho.di.ui.trans.steps.shentong.oss.client;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

public class RequestBody {
	private String method;
	private String bucket;
	private String directory;
	private Map<String, String> metas;
	private byte[] data;
	private String object = "";
	private int contentLength;
	private InputStream inputStream;

	RequestBody(String method, String bucket) {
		this.bucket = bucket;
		this.method = method;
		this.metas = new HashMap<>();
	}

	public static String getObjectInUrl(String path) {
		String objectRebuild = "";
		try {
			path = path.replace('/', '|');
			objectRebuild = URLEncoder.encode(path, "UTF-8");
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
		}
		return objectRebuild;
	}

	public int getContentLength() {
		return this.contentLength;
	}

	public void setContentLength(int contentLength) {
		this.contentLength = contentLength;
	}

	public InputStream getInputStream() {
		return this.inputStream;
	}

	public void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;
	}

	public void addMeta(String key, String value) {
		this.metas.put(key, value);
	}

	public void setDirectory(String directory) {
		this.directory = directory;
	}

	public byte[] getData() {
		if (this.data == null) {
			return null;
		}
		byte[] bytes = new byte[this.data.length];
		System.arraycopy(this.data, 0, bytes, 0, this.data.length);
		return bytes;
	}

	public void setData(byte[] data) {
		this.data = new byte[data.length];
		System.arraycopy(data, 0, this.data, 0, data.length);
	}

	public Map<String, String> getMetas() {
		return this.metas;
	}

	public void setObject(String object) {
		this.object = object;
	}

	public String getMethod() {
		return this.method;
	}

	public String toUrlString() {
		try {
			if (this.directory == null) {
				return "/" + getObjectInUrl(this.object) + "?bucketName=" + URLEncoder.encode(this.bucket, "UTF-8");
			}
			return "/" + this.directory + "?directory&bucketName=" + URLEncoder.encode(this.bucket, "UTF-8");
		} catch (UnsupportedEncodingException localUnsupportedEncodingException) {
		}
		return "";
	}
}
